package com.mass.core.sys.metadata.services.Impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import com.mass.core.sys.metadata.dao.SysMetadataMapper;
import com.mass.core.sys.metadata.model.SysMetadata;
import com.mass.core.sys.metadata.model.SysMetadataExample;
import com.mass.core.sys.metadata.services.SysMetadataServices;
import com.mass.core.utils.AjaxResponse;
import com.mass.core.utils.DateUtils;

@Service
public class SysMetadataServicesImpl implements SysMetadataServices {

	
	@Autowired
	private SysMetadataMapper sysMetadataMapper;
	
	
	/**
	 * 
	* @Title: findSysMetadataPage
	* @Description: 分页条件查询
	* @param sysMetadata
	* @param pageIndex
	* @param pageSize
	* @return
	* AjaxResponse 返回类型   
	* @throws
	 */
	
	public AjaxResponse findSysMetadataPage(SysMetadata sysMetadata,
			Integer pageIndex, Integer pageSize) {
		
		SysMetadataExample sysMetadataExample = new SysMetadataExample();
		SysMetadataExample.Criteria criteria = sysMetadataExample.createCriteria();
		
		criteria.andDelFlagEqualTo(0);//正常
		if(!ObjectUtils.isEmpty(sysMetadata)){
			if(!ObjectUtils.isEmpty(sysMetadata.getMenuId()))   //菜单ID
				criteria.andMenuIdEqualTo(sysMetadata.getMenuId());
			if(!ObjectUtils.isEmpty(sysMetadata.getMetadataName()))//按钮名称
				criteria.andMetadataNameLike("%"+sysMetadata.getMetadataName()+"%");
			if(!ObjectUtils.isEmpty(sysMetadata.getMetadataCode()))
				criteria.andMetadataCodeLike("%"+sysMetadata.getMetadataCode()+"%");
			
		}
		sysMetadataExample.setPageIndex((pageIndex-1)*pageSize);
		sysMetadataExample.setPageSize(pageSize);
		
		List<SysMetadata> sysMList = sysMetadataMapper.selectByExample(sysMetadataExample);
		Long count = sysMetadataMapper.countByExample(sysMetadataExample);
		
		return AjaxResponse.success("ok", sysMList, count, pageIndex);
	}

	/**
	 * 
	* @Title: insert
	* @Description: 添加  
	* @param sysMetadata
	* @return AjaxResponse 返回类型   
	* @throws
	 */
	
	public AjaxResponse insert(SysMetadata sysMetadata) {
		sysMetadata.setCreateTime(DateUtils.getDate());
		sysMetadata.setDelFlag(0);
		int number = sysMetadataMapper.insertSelective(sysMetadata);
		return AjaxResponse.success(number);
	}

	/**
	 * 
	* @Title: update
	* @Description: 修改  
	* @param sysMetadata
	* @return  AjaxResponse 返回类型   
	* @throws
	 */
	
	public AjaxResponse update(SysMetadata sysMetadata) {
		sysMetadata.setLastUpdateTime(DateUtils.getDate());
		int number = sysMetadataMapper.updateByPrimaryKeySelective(sysMetadata);
		return AjaxResponse.success(number);
	}

	/**
	 * 
	* @Title: oneButtonAdded
	* @Description: 一键新增按钮
	* @param menuName   模块名称
	* @param menuId   模块Id
	* @return AjaxResponse 返回类型   
	* @throws
	 */
	
	@Transactional
	public AjaxResponse oneButtonAdded(String menuName, Long menuId) {
		//添加
		SysMetadata addSysMetadata 
		= new SysMetadata(menuName.substring(0, 2)+"新增", "add", null, 2, ".btn", menuId, menuName+"新增按钮");
		
		//修改
		SysMetadata editSysMetadata 
		= new SysMetadata(menuName.substring(0, 2)+"修改", "edit", "iconfont icon-edit", 1, "表格标识ID", menuId, menuName+"修改按钮");
		
		//删除
		SysMetadata delSysMetadata
		= new SysMetadata(menuName.substring(0, 2)+"删除", "del", "iconfont icon-del", 1, "表格标识ID", menuId, menuName+"删除按钮");
		
		this.insert(addSysMetadata);
		
		this.insert(editSysMetadata);
		
		return this.insert(delSysMetadata);
	}

}
